﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using BookShop.DAL;
using BookShop.DTO;
using BookShop.Library.Constant;

namespace BookShop.BLL
{
    public class AuthorService
    {
        public static Author Add(string name, string birthdate, string biography, string pictureUrl)
        {
            using (var repository = new AuthorRepository())
            {
                var existedAuthor = GetByName(name);
                if (existedAuthor != null)
                {
                    return null;
                }
                var author = new Author
                {
                    Name = name,
                    Biography = biography,
                    PictureUrl = pictureUrl,
                    Status = AuthorStatus.Active
                };
                DateTime newBirthdate;
                var isValidBirthdate = DateTime.TryParse(birthdate, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newBirthdate);
                if (isValidBirthdate)
                {
                    author.Birthdate = newBirthdate;
                }
                return repository.Add(author);
            }
        }

        public static Author Update(int id, string name, string birthdate, string biography, string pictureUrl, int status)
        {
            using (var repository = new AuthorRepository())
            {
                var author = GetById(id);
                if (author == null)
                {
                    return null;
                }
                if (!string.IsNullOrWhiteSpace(name))
                {
                    author.Name = name;
                }
                DateTime newBirthdate;
                var isValidBirthdate = DateTime.TryParse(birthdate, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newBirthdate);
                if (isValidBirthdate)
                {
                    author.Birthdate = newBirthdate;
                }
                if (!string.IsNullOrWhiteSpace(biography))
                {
                    author.Biography = biography;
                }
                if (!string.IsNullOrWhiteSpace(pictureUrl))
                {
                    author.PictureUrl = pictureUrl;
                }
                author.Status = status;
                return repository.Update(author);
            }
        }

        public static bool Delete(int id)
        {
            using (var repository = new AuthorRepository())
            {
                var author = GetById(id);
                if (author == null)
                {
                    return false;
                }
                author.Status = AuthorStatus.Inactive;
                author = repository.Update(author);
                return author != null;
            }
        }

        public static List<Author> GetAll()
        {
            using (var repository = new AuthorRepository())
            {
                return repository.FindAll(a => a.Status == AuthorStatus.Active).OrderBy(a => a.Name).ToList();
            }
        }

        public static Author GetById(int authorId)
        {
            using (var repository = new AuthorRepository())
            {
                return repository.Find(a => a.Id == authorId);
            }
        }

        public static Author GetByName(string name)
        {
            using (var repository = new AuthorRepository())
            {
                return repository.Find(a => a.Name.Equals(name.Trim().ToLower()));
            }
        }

        public static List<Author> Search(int? id, string name, string fromBirthdate, string toBirthdate, string biography,
            int? status, int page, int pageSize, out int count)
        {
            var repository = new AuthorRepository();
            var returnList = new List<Author>();
            if (id.HasValue)
            {
                var author = GetById(id.Value);
                returnList.Add(author);
            }
            else
            {
                returnList = repository.GetAll().ToList();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    returnList = returnList.FindAll(a => a.Name.ToLower().Contains(name.Trim().ToLower()));
                }
                DateTime newFromBirthdate;
                var isValidFromBirthdate = DateTime.TryParse(fromBirthdate, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newFromBirthdate);
                DateTime newToBirthdate;
                var isValidToBirthdate = DateTime.TryParse(toBirthdate, CultureInfo.CreateSpecificCulture("vi-VN"),
                    DateTimeStyles.None, out newToBirthdate);
                if (isValidFromBirthdate && isValidToBirthdate)
                {
                    returnList =
                        returnList.FindAll(a => a.Birthdate >= newFromBirthdate && a.Birthdate <= newToBirthdate);
                }
                if (isValidFromBirthdate && !isValidToBirthdate)
                {
                    returnList =
                        returnList.FindAll(a => a.Birthdate >= newFromBirthdate);
                }
                if (!isValidFromBirthdate && isValidToBirthdate)
                {
                    returnList =
                        returnList.FindAll(a => a.Birthdate <= newToBirthdate);
                }
                if (!string.IsNullOrWhiteSpace(biography))
                {
                    returnList = returnList.FindAll(a => a.Biography.Contains(biography));
                }
                if (status.HasValue)
                {
                    returnList = returnList.FindAll(a => a.Status == status.Value);
                }
            }
            count = returnList.Count;
            return returnList.Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }
    }
}
